﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Dala.Samples.form_view
{
    public partial class form_view_with_ods : System.Web.UI.Page
    {
        #region "Select handlers"

        protected void odsDetails_Selecting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            string editMode = Page.RouteData.Values["mode"] as string;

            if (editMode == "view")
                formView.ChangeMode(FormViewMode.ReadOnly);
            else if (editMode == "edit")
                formView.ChangeMode(FormViewMode.Edit);
            else if (editMode == "new")
                formView.ChangeMode(FormViewMode.Insert);
            else
                throw new InvalidOperationException("Invalid Edit Mode.");

            if (formView.CurrentMode == FormViewMode.Insert)
                e.Cancel = true;

        }

        #endregion

        #region "Basic Event Handlers"

        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            if (formView.CurrentMode == FormViewMode.Insert)
            {
                formView.InsertItem(true);
            }
            else
            {
                formView.UpdateItem(true);
            }
        }


        #endregion

        #region "Insert handlers"



        protected void odsDetails_Inserted(object sender, System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs e)
        {
            int result = Convert.ToInt32(e.ReturnValue, System.Globalization.CultureInfo.CurrentCulture.NumberFormat);

            if (result != 0)
            {

                Response.RedirectToRoute(new { mode = "edit", id = result.ToString() });
            }
        }



        #endregion

    }
}